<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="模板名称" prop="templateName">
        <el-input
          v-model="queryParams.templateName"
          placeholder="请输入模板名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="文件名" prop="fileName">
        <el-input
          v-model="queryParams.fileName"
          placeholder="请输入文件名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >删除</el-button>
      </el-col>
<!--      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
        >导出</el-button>
      </el-col> -->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="templateList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="模板名称" align="center" prop="templateName" />
      <el-table-column label="文件名" align="center" prop="fileName" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['template:management:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['template:management:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改模板管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="80%" append-to-body>
     <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="模板名称" prop="templateName">
          <el-input v-model="form.templateName" placeholder="请输入模板名称" />
        </el-form-item>
        <el-form-item label="模板内容" prop="templateCode">
          <Vue2AceEditor
                v-model="form.templateCode"
                @init="editorInit"
                lang="java"
                theme='monokai'
                :options="editorOptions"
                width="100%"
                height="600"
              />
          <!-- <el-input v-model="form.templateCode" /> -->
        </el-form-item>
<!--        <el-form-item label="包路径" prop="packagePath">
          <el-input v-model="form.packagePath" placeholder="请输入包路径" />
        </el-form-item> -->
        <el-form-item label="文件名" prop="fileName">
          <el-input v-model="form.fileName" placeholder="请输入文件名" />
        </el-form-item>
        <el-form-item label="生成路径" prop="filePath">
          <el-input v-model="form.filePath" placeholder="#{packageName}包路径\#{className}类名\#{businessName}业务名\#{moduleName}模块名" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
    import {
        addTemplate,
        delTemplate,
        getTemplate,
        listTemplate,
        updateTemplate
    } from "@/api/template/template";
    import ImageUpload from '@/components/ImageUpload';
    import FileUpload from '@/components/FileUpload';
    import Editor from '@/components/Editor';

export default {
  name: "Template",
  components: {
    Vue2AceEditor: require('vue2-ace-editor'),
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 模板管理表格数据
      templateList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        id: null,
        templateName: null,
        templateCode: null,
        packagePath: null,
        fileName: null,
        filePath: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        delFlag: null
      },
      // 表单参数
      form: {},
      // 表单校验
      // 表单校验
      rules: {
        templateName: [
          { required: true, message: "模板名称不能为空", trigger: "blur" }
        ],
        templateCode: [
          { required: true, message: "模板内容不能为空", trigger: "change" }
        ],
        fileName: [
          { required: true, message: "文件名不能为空", trigger: "change" }
        ],
        filePath: [
          { required: true, message: "生成路径不能为空", trigger: "change" }
        ],
      },
      editorOptions: {
              // 设置代码编辑器的样式
              enableBasicAutocompletion: true,
              enableSnippets: true,
              enableLiveAutocompletion: true,
              tabSize: 2,
              fontSize: 16,
              showPrintMargin: false //去除编辑器里的竖线
            }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    editorInit: function() {
          require("brace/theme/chrome");
          require("brace/theme/github");
          require("brace/theme/eclipse");
          require("brace/theme/monokai");
          require("brace/theme/textmate");
          require("brace/ext/language_tools"); //language extension prerequsite...
          require("brace/mode/yaml");
          require("brace/mode/json");
          require("brace/mode/less");
          require("brace/mode/java");
          require("brace/snippets/json");
          require("brace/snippets/java");
        },
    /** 查询模板管理列表 */
    getList() {
      this.loading = true;
      listTemplate(this.queryParams).then(response => {
        this.templateList = response.data;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        templateName: null,
        templateCode: require('js-base64').Base64.decode('IyMg5qih5p2/5Y+Y6YeP5L+h5oGv6K+05piOIC0tDQojIyAke3RwbENhdGVnb3J5fSDnlJ/miJDnmoTnsbvlnosNCiMjICR7dGFibGVOYW1lfSDmlbDmja7ooajlkI3np7ANCiMjICR7ZnVuY3Rpb25OYW1lfSDnqIvluo/lkI3np7ANCiMjICR7Q2xhc3NOYW1lfSDnsbvlkI0t6aaW5a2X5q+N5aSn5YaZDQojIyAke2NsYXNzTmFtZX0g57G75ZCNLemmluWtl+avjeWwj+WGmQ0KIyMgJHttb2R1bGVOYW1lfSDmqKHlnZflkI3np7ANCiMjICR7QnVzaW5lc3NOYW1lfSDkuJrliqHlkI3np7AgLemmluWtl+avjeWkp+WGmQ0KIyMgJHtidXNpbmVzc05hbWV9IOS4muWKoeWQjeensCAt6aaW5a2X5q+N5bCP5YaZDQojIyAke2Jhc2VQYWNrYWdlfSDljIXliY3nvIAg5aaCIHh4eC54eC54IOeahCB4eHgNCiMjICR7cGFja2FnZU5hbWV9IOWMhei3r+W+hA0KIyMgJHthdXRob3J9IOS9nOiAhQ0KIyMgJHtkYXRldGltZX0g5b2T5YmN5pe26Ze0Leaooeadv+i/kOihjOaXtueahOaXtumXtA0KIyMgJHtwa0NvbHVtbn0g5Li76ZSu5a2X5q61DQojIyAke2ltcG9ydExpc3R9IOmcgOimgeWvvOWFpeWMheeahOWIl+ihqA0KIyMg5aaC5p6c5a2X5q6157G75Z6L5YyF5ZCrIExPQ0FMX0RBVEVfVElNRSDliJnlr7zlhaUgamF2YS50aW1lLkxvY2FsRGF0ZVRpbWUgY29tLmZhc3RlcnhtbC5qYWNrc29uLmFubm90YXRpb24uSnNvbkZvcm1hdA0KIyMg5aaC5p6c5omA5pyJ5YiX5YyF5ZCrQmlnRGVjaW1hbCDliJnlr7zlhaVqYXZhLm1hdGguQmlnRGVjaW1hbA0KIyMg5aaC5p6c55Sf5oiQ55qE57G75Z6L5piv5piv5pWw5o2u57uT5p6E5YiZ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W1l5pel5pyf5o6n5Lu244CBaW1hZ2Xlm77niYfkuIrkvKDmjqfku7bjgIF1cGxvYWTmlofku7bkuIrkvKDmjqfku7bjgIFlZGl0b3Llr4zmlofmnKzmjqfku7bvvIkNCiAjIyAgICDlrZflhbjnsbvlnosgZGljdFR5cGUNCiAjIyAgICDmjpLluo8gICBzb3J0'),
        fileName: null,
        filePath: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        delFlag: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加模板管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getTemplate(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改模板管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateTemplate(this.form).then(response => {
              console.log(this.form)
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addTemplate(this.form).then(response => {
              console.log(this.form)
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除模板管理编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delTemplate(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('template/management/export', {
        ...this.queryParams
      }, `template_management.xlsx`)
    }
  }
};
</script>
